home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 1095 < prev    next >
Encoding:
Text File  |  1996-08-06  |  5.5 KB  |  110 lines

  1. Path: fido.asd.sgi.com!austern
  2. From: kanze@lts.sel.alcatel.de (James Kanze US/ESC 60/3/141 #40763)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: Rationale behind disallowal of non-const r
  5. Date: 15 Apr 1996 11:47:11 PDT
  6. Organization: GABI Software, Sarl.
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <KANZE.96Apr15204435@slsvgqt.lts.sel.alcatel.de>
  9. References: <DAVEW.96Mar27195129@trigati.cs.haverford.edu> <4jevb1$kkp@engnews1.Eng.Sun.COM> <DAVEW.96Apr3110726@trigati.cs.haverford.edu>
  10. NNTP-Posting-Host: isolde.mti.sgi.com
  11. X-Original-Date: 15 Apr 1996 18:44:34 GMT
  12. In-Reply-To: davew@trigati.cs.haverford.edu's message of 03 Apr 1996 09:24:06 PST
  13. Apparently-To: std-c++@ncar.ucar.edu
  14. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  15.     iQBVAwUBMXKZsky4NqrwXLNJAQFvXgIAzVDquyMiPdJbg9092iV1R9orJvhuy/1a
  16.     Op2T2iN0M+kYVL6DBq0ifc3U0Co/6Nooh4ii5OCf68CZJElx8hNxaA==
  17.     =8CFr
  18. Originator: austern@isolde.mti.sgi.com
  19.  
  20. In article <DAVEW.96Apr3110726@trigati.cs.haverford.edu>
  21. davew@trigati.cs.haverford.edu (David G. Wonnacott) writes:
  22.  
  23. |> In article <4jevb1$kkp@engnews1.Eng.Sun.COM> clamage@Eng.sun.com (Steve Clamage) writes:
  24.  
  25. |>    From: clamage@Eng.sun.com (Steve Clamage)
  26. |>    Newsgroups: comp.std.c++
  27. |>    Date: 28 Mar 1996 21:16:23 GMT
  28.  
  29. |>    In article 96Mar27195129@trigati.cs.haverford.edu, davew@trigati.cs.haverford.edu (David G. Wonnacott) writes:
  30. |>    >
  31. |>    >I am looking for an explanation for the rationale behind the decision
  32. |>    >of the standards committee to disallow the binding of a non-const
  33. |>    >reference to an rvalue.
  34.  
  35. |>    The basic reason is that it usually is an error.
  36. |>    ...
  37.  
  38. |>    Yes, there are times when you would like to be able to bind an rvalue
  39. |>    to a non-const reference, since the only things you care about are
  40. |>    sure to happen anyway.        ...           I believe the C++
  41. |>    committee looked for ways to allow this binding of an rvalue to a non-const
  42. |>    reference in cases where the results were what was intended, but failed to
  43. |>    find a good way to define those cases.
  44.  
  45. |> What ever happened to "make it legal but recommend a warning?"  My
  46. |> understanding of "illegal" is that the compiler should refuse to
  47. |> compile the program.  I agree that the vast majority of occurances of
  48. |> a non-const reference to an rvalue would be due to programmer errors.
  49. |> But then again, so are many cases of "if (x=5) ...".
  50.  
  51. My understanding is that what happens in the case of "illegal" programs
  52. is not defined by the standard.  For a certain category of errors, the
  53. standard states that the compiler must issue a diagnostic; once that
  54. occurs, you have undefined behavior.  (I think that we all would agree,
  55. however, that it would be a pretty poor implementation that would go on
  56. and do something bad having recognized an error.  But as some one once
  57. pointed out to me in email: the `diagnostic' doesn't have to be on the
  58. screen.  The implementor is free to document something to the effect
  59. that `the diagnositic for errors requiring such in the standard is to
  60. cause the light on the front of the hard disk to light up for 45
  61. sec./Megabyte storage capacity.)
  62.  
  63. |> As I said, we make use of this all over the place, and its going to be
  64. |> a real problem for us if this produces an error from most compilers.
  65.  
  66. |> We have a class for which (1) copying can be expensive, and (2) there
  67. |> are some operations that extract information only after having a side
  68. |> effect that can not be avoided (without copying).  Imagine if we could
  69. |> do ++i but not i+1 for integers, and that copying them was expensive.
  70. |> Suppose we we have a library that provides functions f1 and f2:
  71.  
  72. |>    int f1()         { ... }
  73. |>    int f2(int &i)   { ++i; return i; }
  74.  
  75. |> (note that we copy the return values, but not the argument - it turns
  76. |> out that we are more concerned with unnecessary copying for arguments
  77. |> than return values).  The user of our library can then use y=f2(x),
  78. |> and x will be incremented, and y will take on the new value.  We'd
  79. |> like to let them use y=f2(f1()), to make y take on the value that is
  80. |> one more than the value returned by f1().  The side-effect that was
  81. |> performed on the object returned from f1 is lost, but we don't care.
  82.  
  83. |> I don't see any way of writing this without either (a) binding a
  84. |> non-const reference to an rvalue, (b) forcing the user of our library
  85. |> to set up an explicit temporary variable every time they use this
  86. |> construct, or (c) giving up on avoiding this overhead and using call
  87. |> by value for f2, or (d) doing something really brain-damaged like:
  88.  
  89. |>    int f2(const int &j) { int &i = (int &)j; i++; return i; }
  90.  
  91. |> But (a) is illegal, (b) is unreasonably inconvenient for our users,
  92. |> (c) is inefficient, and (d) makes me queasy.
  93.  
  94. In the case of `int', I don't think there are any good choices.  But in
  95. the case of `int', I don't think you need them.  For a user defined
  96. class, I would certainly look into what could be done with mutable, and
  97. declaring the parameters constant.
  98. -- 
  99. James Kanze         Tel.: (+33) 88 14 49 00        email: kanze@gabi-soft.fr
  100. GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
  101. Conseils, Θtudes et rΘalisations en logiciel orientΘ objet --
  102.                 -- A la recherche d'une activitΘ dans une region francophone
  103. ---
  104. [ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
  105.                 newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  106.   comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  107.   Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  108.   Comments? mailto:std-c++-request@ncar.ucar.edu 
  109. ]
  110.